/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.entityGenerator.base.elements;

import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.field.FieldGenerator;
import com.inspur.edp.caf.generator.item.ClassGenerator;
import com.inspur.edp.caf.generator.item.ClassGeneratorContext;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.caf.generator.property.ClassPropertyGenerator;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.element.GspAssociation;
import com.inspur.edp.cef.entity.entity.AssoInfoBase;
import com.inspur.edp.cef.gencommon.CefPropertyInfo;
import com.inspur.edp.cef.gencommon.DataEqualsMethodGenerator;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.entityGenerator.base.DataTypeGeneratorContext;
import com.inspur.edp.cef.generator.entityGenerator.base.elements.method.AssoCreateGenerator;
import com.inspur.edp.cef.generator.entityGenerator.base.elements.method.AssoEqualsMethodGenerator;
import com.inspur.edp.cef.generator.entityGenerator.base.elements.method.AssoGetValueGenerator;
import com.inspur.edp.cef.generator.entityGenerator.base.elements.method.AssoSetValueGenerator;
import com.inspur.edp.cef.spi.entity.IAuthFieldValue;
import lombok.var;
import org.eclipse.jdt.core.dom.Modifier;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class ElementAssoGenerator extends ClassGenerator {
  private IGspCommonField field;
  private GspAssociation association;
  private DataTypeGeneratorContext dataTypeGenContext;
  private ArrayList<ClassPropertyGenerator> propertyGenerators;

  public ElementAssoGenerator(
      IGspCommonField field,
      GspAssociation association,
      DataTypeGeneratorContext dataTypeGenContext) {
    super(dataTypeGenContext.getEntityGenContext().getCompilationUnitInfo());
    this.field = field;
    this.association = association;
    this.dataTypeGenContext = dataTypeGenContext;
  }

  protected IGspCommonField getField() {
    return field;
  }

  @Override
  protected ArrayList<FieldGenerator> createFieldGenerator() {
    ArrayList<FieldGenerator> list = super.createFieldGenerator();
    if(list ==null)
      list =new ArrayList<FieldGenerator>();

    for (IGspCommonField item : association.getRefElementCollection()) {
      list.add(new AssoFieldGenerator(item, association, dataTypeGenContext));
    }
    list.add(0, new AssoFieldGenerator(field, association, dataTypeGenContext));
    return list;
  }

  // region Properties
  @Override
  protected ArrayList<ClassPropertyGenerator> createPropertyGenerators() {
    propertyGenerators = new ArrayList<ClassPropertyGenerator>();
    for (IGspCommonField item : association.getRefElementCollection()) {
      propertyGenerators.add(new AssoPropertyGenerator(item, association, dataTypeGenContext));
    }
    propertyGenerators.add(0, new AssoPropertyGenerator(field, association, dataTypeGenContext));
    return propertyGenerators;
  }
  // endregion

  @Override
  protected ClassGeneratorContext createClassInfoContext() {
    return new ClassGeneratorContext();
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getAccessModifier() {
    return new ArrayList<Modifier.ModifierKeyword>(
        Arrays.asList(new Modifier.ModifierKeyword[] {Modifier.ModifierKeyword.PUBLIC_KEYWORD}));
  }

  @Override
  protected void doInitialize() {
    super.doInitialize();
    dataTypeGenContext
        .getEntityGenContext()
        .getElementAssociationTypes()
        .put(field.getID(), new MediateType(dataTypeGenContext.getEntityGenContext().getDefaultPackageName(), getName()));
  }

  public static final String nameSuffix = "Info";

  @Override
  protected String getName() {
    return field.getLabelID() + nameSuffix;
  }

  @Override
  protected void afterGenerate() {
    super.afterGenerate();
    dataTypeGenContext.getAssociationTypes().put(field.getLabelID(), getName());
  }

  private ArrayList<CefPropertyInfo> getPropertyInfos() {
    ArrayList<CefPropertyInfo> propertyInfos=new ArrayList<>();
    for (IGspCommonField item : association.getRefElementCollection())
    {
      propertyInfos.add(new CefPropertyInfo(item.getLabelID(),null));

    }
    propertyInfos.add(new CefPropertyInfo(field.getLabelID(),null));
    return propertyInfos;
  }

  @Override
  protected ArrayList<ClassMethodGenerator> createMethodGenerators() {
    ArrayList<ClassMethodGenerator> list = new ArrayList<ClassMethodGenerator>(
        Arrays.asList(new ClassMethodGenerator[] {
                new GetValueMethodGenerator(field),
                //new AssoEqualsMethodGenerator(),
                new AssoGetValueGenerator(propertyGenerators),
                new AssoSetValueGenerator(propertyGenerators, dataTypeGenContext)
        }));

    AssoCreateGenerator assoCreateGenerator=
    new AssoCreateGenerator(propertyGenerators, association, dataTypeGenContext);
    if(assoCreateGenerator.isHasCase())
      list.add(assoCreateGenerator);
    return list;
  }

  @Override
  protected ArrayList<TypeInfo> addParentInterfaces() {
    TypeInfo interfaceType =new  TypeInfo(IAuthFieldValue.class);
    return new ArrayList<TypeInfo>(
        Arrays.asList(new TypeInfo[] {interfaceType}));

  }

  public static String getName(IGspCommonField field) {
    return field.getLabelID() + nameSuffix;
  }

  @Override
  protected TypeInfo setParentClass() {
    return new TypeInfo(AssoInfoBase.class);
  }
}
